﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace BSC_OTC_Management.Forms
{
    public partial class Frm_Orgorder_manage : Form
    {
        public Frm_Orgorder_manage(Hashtable _v_HtbSubfund, Hashtable _v_HtbSymbol, DataTable _v_dtbSymbolInfo)
        {
            InitializeComponent();
            this.v_HtbSubfund = _v_HtbSubfund;
            this.v_HtbSymbol = _v_HtbSymbol;
            this.v_dtbSymbolInfo = _v_dtbSymbolInfo;
        }

        #region "KHAI BAO BIEN TOAN CUC"

        Code.CL_Ultility _cl = new Code.CL_Ultility();
        string v_CompanyName, v_FundName, v_SubFunName, v_Custodycd, v_Side, V_Symbol, V_LimitQuantity, v_LimitPrice;
        Code.StaticVariable _code = new Code.StaticVariable();
        Hashtable v_HtbSubfund = new Hashtable();
        Hashtable v_HtbSymbol = new Hashtable();
        DataTable v_dtbSymbolInfo = new DataTable();
        int _oldPrice = 0;
        int _oldqty = 0;
        #endregion

        #region "KHAI BAO CAC THUOC TINH CUA FORM"

        public string v_FormType;

        public string Get_FormType
        {
            get { return v_FormType; }
            set { v_FormType = value; }
        }

        public DataTable v_dtbCompanyInfo;

        public DataTable Get_dtbCompanyInfo
        {
            get { return v_dtbCompanyInfo; }
            set { v_dtbCompanyInfo = value; }
        }

        public string Get_CompanyName
        {
            get { return v_CompanyName; }
            set { v_CompanyName = value; }
        }

        public string Get_FundName
        {
            get { return v_FundName; }
            set { v_FundName = value; }
        }

        public string Get_SubFunName
        {
            get { return v_SubFunName; }
            set { v_SubFunName = value; }
        }

        public string Get_Custodycd
        {
            get { return v_Custodycd; }
            set { v_Custodycd = value; }
        }

        public string Get_Symbol
        {
            get { return V_Symbol; }
            set { V_Symbol = value; }
        }

        public string Get_Side
        {
            get { return v_Side; }
            set { v_Side = value; }
        }

        public string Get_LimitQuantity
        {
            get { return V_LimitQuantity; }
            set { V_LimitQuantity = value; }
        }

        public string Get_LimitPrice
        {
            get { return v_LimitPrice; }
            set { v_LimitPrice = value; }
        }


        #endregion

        #region "KHAI BAO CAC HÀM TRONG FORM"

        //private void Fill_HashTable(Hashtable v_HashTable, DataTable v_DataTable, ComboBox v_CbbName)
        //{
        //    try
        //    {
        //        v_CbbName.Items.Clear();
        //        foreach (DataRow row in v_DataTable.Rows)
        //        {
        //            v_HashTable.Add(row["KeyName"].ToString(), row["KeyValue"].ToString());
        //            v_CbbName.Items.Add(row["KeyValue"].ToString());
        //        }
        //    }
        //    catch
        //    { }

        //}

        public void Set_value_to_Combobox(DataTable v_TBL, ComboBox cbb_name, string Collum_name, string v_strDK)
        {
            try
            {
                DataView dv = new DataView(v_TBL);
                dv.RowFilter = v_strDK;
                DataTable dbt_temp = new DataTable();
                dbt_temp = dv.ToTable();
                cbb_name.Items.Clear();
                for (int i = 0; i < dbt_temp.Rows.Count; i++)
                {
                    string v_item = dbt_temp.Rows[i][Collum_name].ToString();
                    if (cbb_name.Items.Contains(v_item) != true)
                    {
                        cbb_name.Items.Add(dbt_temp.Rows[i][Collum_name].ToString());
                        cbb_name.Text = dbt_temp.Rows[i][Collum_name].ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        public void Set_value_to_ComboboxBK(DataTable v_TBL, ComboBox cbb_name, string Collum_name, string v_strDK)
        {
            try
            {
                DataView dv = new DataView(v_TBL);
                dv.RowFilter = v_strDK;
                DataTable dbt_temp = new DataTable();
                dbt_temp = dv.ToTable();

                cbb_name.Items.Clear();
                int l = dbt_temp.Rows.Count;
                for (int i = 0; i < l; i++)
                {
                    string v_item = dbt_temp.Rows[i][Collum_name].ToString();
                    if (cbb_name.Items.Contains(v_item) != true)
                    {
                        cbb_name.Items.Add(dbt_temp.Rows[i][Collum_name].ToString());
                    }

                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        public void LOAD_FORM()
        {
            try
            {
                switch (v_FormType)
                {
                    case "ADD":
                        this.Text = "Thêm mới lệnh chỉ thị";
                        _cl.Set_value_to_Combobox(this.v_dtbSymbolInfo, this.cbb_SymBol, "KeyName", "");
                        Set_value_to_Combobox(v_dtbCompanyInfo, this.cbb_SubFundName, "subfundname", "");

                        break;
                    case "EDIT":
                        this.Text = "Sửa lệnh chỉ thị";
                        this.cbb_CompanyName.Enabled = false;
                        this.cbb_CompanyName.Text = Get_CompanyName;
                        this.cbb_FundName.Enabled = false;
                        this.cbb_FundName.Text = Get_FundName;
                        this.cbb_SubFundName.Enabled = false;
                        this.cbb_SubFundName.Text = Get_SubFunName;
                        this.cbb_Custodycd.Enabled = false;
                        this.cbb_Custodycd.Text = Get_Custodycd;
                        this.cbb_Side.Enabled = false;
                        this.cbb_Side.Text = Get_Side;
                        this.cbb_SymBol.Enabled = false;
                        this.cbb_SymBol.Text = Get_Symbol;
                        this.cbb_LimitQuantty.Text = Get_LimitQuantity.Replace(",", "");
                        this.cbb_LimitPrice.Text = (Convert.ToDouble( Get_LimitPrice)  ).ToString();
                        this._oldPrice = Convert.ToInt32( this.cbb_LimitPrice.Text);
                        this._oldqty = Convert.ToInt32( this.cbb_LimitQuantty.Text);
                        break;
                    case "DELT":
                        this.Text = "Hủy lệnh chỉ thị";
                        this.cbb_CompanyName.Enabled = false;
                        this.cbb_CompanyName.Text = Get_CompanyName;
                        this.cbb_FundName.Enabled = false;
                        this.cbb_FundName.Text = Get_FundName;
                        this.cbb_SubFundName.Enabled = false;
                        this.cbb_SubFundName.Text = Get_SubFunName;
                        this.cbb_Custodycd.Enabled = false;
                        this.cbb_Custodycd.Text = Get_Custodycd;
                        this.cbb_Side.Enabled = false;
                        this.cbb_Side.Text = Get_Side;
                        this.cbb_SymBol.Enabled = false;
                        this.cbb_SymBol.Text = Get_Symbol;
                        this.cbb_LimitQuantty.Text = Get_LimitQuantity.Replace(",", "");
                        this.cbb_LimitQuantty.Enabled = false;
                        // double v_limit = double.Parse(Get_LimitPrice);
                        this.cbb_LimitPrice.Text = (Convert.ToDouble(Get_LimitPrice) ).ToString();
                        this.cbb_LimitPrice.Enabled = false;
                        break;

                }
            }
            catch
            {
            }
        }

        #endregion

        private void Frm_Orgorder_manage_Load(object sender, EventArgs e)
        {
            LOAD_FORM();
        }

        #region "CAC SỰ KIỆN TRÊN CONTROL CỦA FORM"

        private void cbb_Side_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbb_Side.Text.ToUpper() == "B")
            {
                this.lbl_Side.Text = "Mua";
                this.BackColor = Color.FromArgb(167, 204, 240);


            }
            else if (cbb_Side.Text.ToUpper() == "S")
            {
                this.lbl_Side.Text = "Bán";
                this.BackColor = Color.FromArgb(245, 197, 217);

            }
            else
            {
                this.lbl_Side.Text = "";
            }

        }

        #endregion


        private void btn_XacNhan_Click(object sender, EventArgs e)
        {
            try
            {
                double pv_LimitPrice, pv_limitqty;
                double pv_gia_san, pv_Gia_tran;
                string pv_symbol, pv_Side, v_Cmd, v_Cmd_insertHist;
                pv_Side = this.cbb_Side.Text;
                pv_symbol = this.cbb_SymBol.Text;
                if (pv_Side.Trim() == "")
                {
                    MessageBox.Show("[Mua/Bán]-Chưa chọn loại lệnh ! ", "Thông báo");
                    this.cbb_Side.Focus();
                    return;
                }

                if (pv_symbol.Trim() == "")
                {
                    MessageBox.Show("[SYMBOL]-Chưa chọn mã chứng khoán! ", "Thông báo");
                    this.cbb_SymBol.Focus();
                    return;
                }

                try
                {
                    pv_limitqty = Double.Parse(this.cbb_LimitQuantty.Text);
                    if (pv_limitqty <= 0)
                    {
                        MessageBox.Show("Khối lượng không hợp lệ", "Thông báo");
                        this.cbb_LimitQuantty.Focus();
                        return;
                    }
                }
                catch
                {
                    MessageBox.Show("Khối lượng không hợp lệ", "Thông báo");
                    this.cbb_LimitQuantty.Focus();
                    return;
                }

                try
                {
                    pv_LimitPrice = Double.Parse(this.cbb_LimitPrice.Text) * 1000;
                    pv_gia_san = double.Parse(this.lbl_Gia_san.Text);
                    pv_Gia_tran = double.Parse(this.lbl_Gia_tran.Text);

                    if (pv_LimitPrice < pv_gia_san || pv_LimitPrice > pv_Gia_tran)
                    {
                        MessageBox.Show("Giá nhập phải nằm trong khoảng trần sàn", "Thông báo");
                        return;
                    }
                }
                catch
                {
                    MessageBox.Show("Giá không hợp lệ", "Thông báo");
                    this.cbb_LimitPrice.Focus();
                    return;
                }

                if (cbb_Custodycd.Text.Trim() == "")
                {
                    MessageBox.Show("Số tài khoản không hợp lệ", "Thông báo");
                    this.cbb_Custodycd.Focus();
                    return;
                }


                // Don vi gia 10000
                //pv_LimitPrice = pv_LimitPrice * 1000;

                switch (v_FormType)
                {
                    case "ADD":
                        string V_subFundID = _cl.Get_key_HashTable(this.v_HtbSubfund, this.cbb_SubFundName.Text);
                        if (V_subFundID.Trim() == "")
                        {
                            MessageBox.Show("Kiểm tra lại thông tin quỹ", "Thông báo");
                            this.cbb_SubFundName.Focus();
                            return;
                        }
                        this.Text = "Thêm mới lệnh chỉ thị";

                        string v_cmdCheck = "Select deltd From bsc_fao_instruction_order where SUBFUNDID ='" + V_subFundID + "' and SIDE='" + pv_Side + "' and SYMBOL='" + pv_symbol + "' and txdate =getcurrdate";
                        DataTable dtb_check = _code._services.GetDatatable_BySQL(v_cmdCheck);

                        if (dtb_check.Rows.Count > 0)
                        {
                            string v_strDelt = dtb_check.Rows[0]["deltd"].ToString();
                            if (v_strDelt == "N")
                            {
                                MessageBox.Show("Quỹ đã có lệnh tương tự trong hệ thống", "Thông báo");
                                return;
                            }
                            else if (v_strDelt == "Y")
                            {
                                DialogResult rs = MessageBox.Show("Quỹ đã có lệnh tương ứng [ở trạng thái xóa] trong hệ thống, bạn có muốn kích hoạt lại ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                                if (rs == DialogResult.No)
                                {
                                    return;
                                }
                                else
                                {
                                    v_Cmd = @"Update  bsc_fao_instruction_order set  deltd='N'  , limitqty =" + pv_limitqty + "  , limitprice = " + pv_LimitPrice +
                                  " Where 0=0 " +
                                  " And txdate =getcurrdate " +
                                  " And symbol ='" + this.cbb_SymBol.Text + "'" +
                                  " And side ='" + this.cbb_Side.Text + "'" +
                                  " And subfundid = (Select subfundid From bsc_fao_subfunds_info where subfundname='" + this.cbb_SubFundName.Text + "') ";
                                    _code._services.Excute_DB(v_Cmd);
                                    MessageBox.Show("Kích hoạt lại lệnh chỉ thị thành công", "Thông báo");
                                    this.Close();
                                    break;
                                    
                                }

                            }
                        }

                        v_Cmd = @"INSERT INTO bsc_fao_instruction_order (INSORDERID,SUBFUNDID,SIDE,SYMBOL,LIMITQTY,LIMITPRICE,STATUS,DELTD,TLID,TXDATE,LAST_CHANGE,DESCRIPTION)" +
                                      "VALUES(seq_insorderid.nextval ," + V_subFundID + ",'" + pv_Side + "','" + pv_symbol + "'," + pv_limitqty + "," + pv_LimitPrice + ",'A','N','1024',getcurrdate,sysdate,NULL)";

                        if (_code._services.Excute_DB(v_Cmd) == true)
                        {
                            MessageBox.Show("Thêm mới lệnh chỉ thị thành công", "Thông báo");
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("Thêm mới lệnh chỉ thị không thành công", "Thông báo");
                        }

                        break;
                    case "EDIT":
                        if (this._oldPrice != pv_LimitPrice || this._oldqty != pv_limitqty)
                        {
                            V_subFundID = _cl.Get_key_HashTable(this.v_HtbSubfund, this.cbb_SubFundName.Text);
                            if (V_subFundID.Trim() == "")
                            {
                                MessageBox.Show("Kiểm tra lại thông tin quỹ", "Thông báo");
                                this.cbb_SubFundName.Focus();
                                return;
                            }
                            this.Text = "Thêm mới lệnh chỉ thị";

                            v_cmdCheck = "Select * From bsc_fao_instruction_order where SUBFUNDID ='" + V_subFundID + "' and SIDE='" + pv_Side + "' and SYMBOL='" + pv_symbol + "' and txdate =getcurrdate";
                            dtb_check = _code._services.GetDatatable_BySQL(v_cmdCheck);
                            if (dtb_check.Rows.Count > 0)
                            {
                                string INSORDERID = Convert.ToString(dtb_check.Rows[0]["INSORDERID"]);
                                v_Cmd_insertHist = @"INSERT INTO bsc_fao_instruction_order_hist (INSORDERID,SUBFUNDID,SIDE,SYMBOL,LIMITQTY,LIMITPRICE,TLID,TXDATE,TXTIME)" +
                                                    "VALUES(" + INSORDERID + "," + V_subFundID + ",'" + pv_Side + "','" + pv_symbol + "'," + Convert.ToString(dtb_check.Rows[0]["LIMITQTY"]) + "," + Convert.ToString(dtb_check.Rows[0]["LIMITPRICE"]) + ",'1024',getcurrdate,sysdate)";
                                _code._services.Excute_DB(v_Cmd_insertHist);
                            }

                            v_Cmd = @"Update bsc_fao_instruction_order set limitqty =" + pv_limitqty + "  , limitprice = " + pv_LimitPrice +
                                    " Where 0=0 " +
                                    " And txdate =getcurrdate " +
                                    " And symbol ='" + this.cbb_SymBol.Text + "'" +
                                    " And side ='" + this.cbb_Side.Text + "'" +
                                    " And subfundid = (Select subfundid From bsc_fao_subfunds_info where subfundname='" + this.cbb_SubFundName.Text + "') ";
                            _code._services.Excute_DB(v_Cmd);

                            MessageBox.Show("Sửa lệnh chỉ thị thành công", "Thông báo");
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("Lệnh sửa không thay đổi", "Thông báo");
                        }
                        break;
                    case "DELT":
                        v_Cmd = @"Update  bsc_fao_instruction_order set deltd='Y' " +
                                     " Where 0=0 " +
                                     " And txdate =getcurrdate " +
                                     " And symbol ='" + this.cbb_SymBol.Text + "'" +
                                     " And side ='" + this.cbb_Side.Text + "'" +
                                     " And subfundid = (Select subfundid From bsc_fao_subfunds_info where subfundname='" + this.cbb_SubFundName.Text + "') ";
                        _code._services.Excute_DB(v_Cmd);
                        MessageBox.Show("Hủy lệnh chỉ thị thành công", "Thông báo");
                        this.Close();
                        break;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void cbb_CompanyName_TextChanged(object sender, EventArgs e)
        {
            //Set_value_to_Combobox(v_dtbCompanyInfo, this.cbb_FundName, "fundname", "companyname='" + this.cbb_CompanyName.Text + "'");
        }

        private void cbb_FundName_TextChanged(object sender, EventArgs e)
        {
            //Set_value_to_Combobox(v_dtbCompanyInfo, this.cbb_SubFundName, "subfundname", "fundname='" + this.cbb_FundName.Text + "'");
        }

        private void cbb_SubFundName_TextChanged(object sender, EventArgs e)
        {
            Set_value_to_Combobox(v_dtbCompanyInfo, this.cbb_CompanyName, "companyname", "subfundname='" + this.cbb_SubFundName.Text + "'");
            Set_value_to_Combobox(v_dtbCompanyInfo, this.cbb_FundName, "fundname", "subfundname='" + this.cbb_SubFundName.Text + "'");
            Set_value_to_Combobox(v_dtbCompanyInfo, this.cbb_Custodycd, "custodycd", "subfundname='" + this.cbb_SubFundName.Text + "'");
        }

        private void cbb_SymBol_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string v_gia = _cl.Get_value_HashTable(this.v_HtbSymbol, this.cbb_SymBol.Text);

                if (v_gia.Trim() != "")
                {
                    string[] v_arrGia = v_gia.Split('|');

                    if (v_arrGia.Length != 0)
                    {
                        lbl_San_giao_dich.Text = v_arrGia[0].ToString();
                        lbl_Gia_san.Text = v_arrGia[1].ToString();
                        lbl_Tham_chieu.Text = v_arrGia[2].ToString();
                        lbl_Gia_tran.Text = v_arrGia[3].ToString();
                    };
                }
                else
                {
                    lbl_San_giao_dich.Text = "";
                    lbl_Gia_san.Text = "";
                    lbl_Tham_chieu.Text = "";
                    lbl_Gia_tran.Text = "";
                }

            }
            catch (Exception ex)
            { }
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbb_SubFundName_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
